﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Bll;
using System.Data;
using AspNetCs.Model;
using AspNetCs.Common;

public partial class Master_Admin_photoAdd : Admin_BasePage
{
    protected UserPhoto UserPhotoBll = new UserPhoto();
    protected string Action = "";
    protected int id = 0;
    protected Cutter cut = new Cutter();


    protected void Page_Load(object sender, EventArgs e)
    {
        IsExit();
        if (Request["Action"] != null)
        {
            Action = Request["Action"].ToString();

        }
        if (Request["id"] != null)
        {
            id = StringHelper.isNumber_int(Request["id"].ToString());
        }

        cut.UpFoler = Admin_UpFolder;
        cut.UpSize = configInfo.UpSize;
        cut.IsNewFile = true;



        if (!Page.IsPostBack)
        {
            if (Action == "Modify")
            {
                if (id <= 0)
                {
                    ShowMsgBack("参数错误!");
                    Response.End();
                }


                classAllbind();

                UserPhoto_Model UserPhotoModel = UserPhotoBll.GetModel(id);
                this.btnSubmit.Visible = false;
                this.Button1.Visible = true;
                this.DD_ClassList.Items.FindByValue(UserPhotoModel.ClassId.ToString()).Selected = true;
                this.Txt_PicName.Text = UserPhotoModel.PhotoName;
                this.Txt_Readme.Text = UserPhotoModel.Readme;
                this.RequiredFieldValidator3.Visible = false;

            }
            else
            {
                classbind();
                ListItem itm = new ListItem("请选择分类名称", "");
                itm.Selected = true;
                this.DD_ClassList.Items.Add(itm);
            }
        }




    }
    protected void classbind()
    {
        DataTable dt = UserPhotoBll.GetDataTable_Class("");
        dt.DefaultView.RowFilter = " id>=3 and id<=9 ";
        this.DD_ClassList.DataSource = dt;
        this.DD_ClassList.DataTextField = "ClassName";
        this.DD_ClassList.DataValueField = "ID";
        this.DD_ClassList.DataBind();
    }

    protected void classAllbind()
    {
        DataTable dt = UserPhotoBll.GetDataTable_Class("");
        this.DD_ClassList.DataSource = dt;
        this.DD_ClassList.DataTextField = "ClassName";
        this.DD_ClassList.DataValueField = "ID";
        this.DD_ClassList.DataBind();
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        UserPhoto_Model model = new UserPhoto_Model();
        model.ClassId = StringHelper.isNumber_int(DD_ClassList.SelectedValue);
        model.CreateId = 0;
        model.PhotoName = Txt_PicName.Text;
        model.IsCheck = 1;
        model.Readme = StringHelper.RemoveHtml(Txt_Readme.Text);
        string Msg = "";
        bool isok = UpLoad_picture.UpSave(FileUpload1, cut, out Msg);
        if (!isok)
        {
            ShowMsgBack(Msg);
            Response.End();
        }
        else
        {
            model.PhotoUrl = Msg;
        }

        if (UserPhotoBll.Add(model) > 0)
        {
            ResultStr("Admin_photolist.aspx", "图片添加成功！");
            Response.End();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        if (id <= 0)
        {
            ShowMsgBack("参数错误!");
            Response.End();
        }


        UserPhoto_Model photoModel = UserPhotoBll.GetModel(id);//
        UserPhoto_Model Model = new UserPhoto_Model();

        Model.Id = id;
        Model.IsCheck = 1;
        Model.ClassId = StringHelper.isNumber_int(DD_ClassList.SelectedValue.ToString());
        Model.PhotoName = Txt_PicName.Text;
        Model.Readme = StringHelper.RemoveHtml(Txt_Readme.Text);
        if (FileUpload1.FileName != "")
        {
            string Msg = "";
            bool isok = UpLoad_picture.UpSave(FileUpload1, cut, out Msg);
            if (!isok)
            {
                ShowMsgBack(Msg);
                Response.End();
            }
            else
            {
                StringHelper.delFile(photoModel.PhotoUrl);
                Model.PhotoUrl = Msg;
            }
        }
        else
        {
            Model.PhotoUrl = photoModel.PhotoUrl;
        }
        if (UserPhotoBll.Update(Model) > 0)
        {
            ResultStr("Admin_photoAdd.aspx?Action=Modify&id=" + id, "图片修改成功！");
            Response.End();
        }
        else
        {
            ShowMsgBack("图片修改失败");
            Response.End();
        }
    }
}
